package com.tongcao.cn.service.impl;

import com.tongcao.cn.base.req.IRequest;
import com.tongcao.cn.domain.ReplaceBeanName;
import com.tongcao.cn.dfire.result.Result;
import com.tongcao.cn.dfire.utils.ResultUtil;
import com.tongcao.cn.executor.IBookerExecutor;
import com.tongcao.cn.executor.IInnerExecutor;
import com.tongcao.cn.processor.BeanHelper;
import com.tongcao.cn.service.IBookerClient;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author:tongcao
 * @Date:2019/1/24 15:56
 * Description:
 */

@Component
public class BookerClient implements IBookerClient {


    @Resource
    private BeanHelper beanHelper;


    @Override
    public <T> Result<T> execute(IRequest<T> request) {
        long t1 = System.currentTimeMillis();
        Result<T> result;
        try {
            /**
             * executor_name
             */
            String beanName = ReplaceBeanName.getName(ReplaceBeanName.REQUEST, ReplaceBeanName.EXECUTOR, request.getClass());
            /**
             * 得到具体执行器
             */
            IBookerExecutor executor = beanHelper.getBean(beanName, IBookerExecutor.class);
            result = executor.execute(request);
            long t2 = System.currentTimeMillis();
        } catch (Exception e) {
            //todo 打印错误日志时间
            result = ResultUtil.failResult("400", "here is throw exception");
            long t2 = System.currentTimeMillis();
        }
        return result;
    }


    @Override
    public <REQ, RSP> RSP bizExecute(REQ request) {
        String beanName = ReplaceBeanName.getName(ReplaceBeanName.REQUEST, ReplaceBeanName.EXECUTOR, request.getClass());

        IInnerExecutor executor = beanHelper.getBean(beanName, IInnerExecutor.class);
        if (executor.isDisable()) {
            //todo
        }
        return (RSP) executor.execute(request);
    }
}
